package kg.aibek.projecteuler.p10;

import java.util.Arrays;

public class Problem44 {
	public void solve() {
		int max = 10000;
		int a[] = new int[max];
		for (int i = 1; i <= max; i++) {
			a[i - 1] = i * (3 * i - 1) / 2;
		}

		int min = Integer.MAX_VALUE;
		for (int i = 0; i < max; i++) {
			for (int j = i + 1; j < max; j++) {
				int diff = a[j] - a[i];
				int summ = a[j] + a[i];
				if (Arrays.binarySearch(a, diff) >= 0
						&& Arrays.binarySearch(a, summ) >= 0) {
					min = Math.min(min, diff);
				}
			}
		}

		System.out.println(min);
	}

	public static void main(String[] args) {
		Problem44 p = new Problem44();
		p.solve();
	}
}
